<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    
    // Redirect to login screen if user not logged in
    if (!$this->session->userdata('login')) redirect('login');
  }
  
  // ===========================================================================

  public function index()
  {
    $data['alert'] = alerts();
    
    $data['page_title']  = 'Settings — Stune';
    $data['standard']    = TRUE;
    
    $data['email'] = $this->session->userdata('email');
    
    $this->load->view('templates/header', $data);
    $this->load->view('settings/settings_view', $data);
    $this->load->view('templates/footer', $data);
  }
  
  // ===========================================================================
  
  public function email()
  {
    if ($this->input->post('email') != '')
    {
      $user_id = $this->session->userdata('user_id');
      
      $email_current = $this->input->post('email_current');
      $data['email'] = $this->input->post('email');
      $password      = $this->input->post('password');
      
      $this->load->model('guests_model');
      
      if ($this->guests_model->validate_password($email_current, $password))
      {
        if ($this->guests_model->validate_email($data['email']))
        {
          $this->guests_model->update_user($data, $user_id);
          $this->session->set_flashdata('alert', 'email y');
          redirect('settings');
        }
        else
        {
          $this->session->set_flashdata('alert', 'email e_n');
          redirect('settings/email');
        }
      }
      else
      {
        $this->session->set_flashdata('alert', 'email p_n');
        redirect('settings/email');
      }
      
      if ($this->guests_model->update_user($data, $user_id))
        $this->session->set_flashdata('alert', 'email y');
      else
        $this->session->set_flashdata('alert', 'email n');
      
      $this->session->set_userdata($data);
      
      redirect('settings');
    }
    
    $data['page_title']  = title('Update Email');
    $data['standard']    = TRUE;
    
    $data['email'] = $this->session->userdata('email');
    
    $data['alert'] = alerts();
    
    $this->load->helper('form');
    
    $this->load->view('templates/header', $data);
    $this->load->view('settings/email', $data);
    $this->load->view('templates/footer', $data);
  }
  
  // ===========================================================================
  
  public function password()
  {
    $data['alert'] = alerts();
    
    if ($this->input->post('password') != '')
    {
      $user_id = $this->session->userdata('user_id');
      
      $password_new     = $this->input->post('password');
      $password_repeat  = $this->input->post('password_repeat');
      
      $email            = $this->session->userdata('email');
      $password_current = $this->input->post('password_current');
      
      $this->load->model('guests_model');
      
      if ($password_new === $password_repeat)
      {
        if ($this->guests_model->validate_password($email, $password_current))
        {
          $this->guests_model->update_password($password_new, $user_id);
          $this->session->set_flashdata('alert', 'password y');
          redirect('settings');
        }
        else
        {
          $this->session->set_flashdata('alert', 'password c_n');
          redirect('settings/password');
        }
      }
      else
      {
        $this->session->set_flashdata('alert', 'password n_n');
        redirect('settings/password');
      }
    }
    
    $data['page_title']  = title('Update Password');
    $data['standard']    = TRUE;
    
    $this->load->helper('form');
    
    $this->load->view('templates/header', $data);
    $this->load->view('settings/password', $data);
    $this->load->view('templates/footer', $data);
  }
  
  // ===========================================================================
  
  public function recharge()
  {
    if ($this->input->post('amount') != '')
    {
      $this->load->model('guests_model');
      
      $data['balance'] = $this->guests_model->get_user_data('balance', $this->session->userdata('email')) + $this->input->post('amount');
      
      $this->guests_model->update_user($data, $this->session->userdata('email'));
      
      redirect('dashboard');
    }
    
    $data['page_title']  = title('Recharge Account');
    $data['standard']    = TRUE;
    
    $this->load->helper('form');
    
    $this->load->view('templates/header', $data);
    $this->load->view('settings/recharge', $data);
    $this->load->view('templates/footer', $data);
  }
}

// End of File